package sdu.attendancechecker.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sdu.attendancechecker.models.TeacherModel;
import sdu.attendancechecker.services.PasswordService;

public class ForgotServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if (command.equals("generatePassword")) {
			String password = PasswordService.generatePassword();
			String email = request.getParameter("email");
			if (email.equals("") || email == null) {
				response.getWriter().println(1);
				return;
			}
			if (TeacherModel.getByEmail(email) != null) {
				TeacherModel.updatePasswordByEmail(email, password);
				PasswordService.sendPasswordTo(email, password);
				response.getWriter().println(0);
				return;
			} else {
				response.getWriter().println(1);
				return;
			}
		}
	}
}
